package com.meraklisina.latexeditor;

import java.io.* ;

import javax.swing.JDialog ;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;



public class TemplateForm extends javax.swing.JPanel {
	private JTabbedPane tab_Editors;
	private JTextPane textPane;
	JDialog topContainer = null;
    JButton buttons[] = new JButton[6];
    JLabel labels[] = new JLabel[6];
    Style styles[] = new Style[6];
    
    String templateNames[] = {
		"Hello World",
		"Resume",
		"Scientific",
		"Letter",
		"Table",
		"Image"
    };
    
    String templateImages[] = {
		"/com/meraklisina/latexeditor/resources/icons/ic_helloworld.png",
		"/com/meraklisina/latexeditor/resources/icons/ic_resume.png",
		"/com/meraklisina/latexeditor/resources/icons/ic_scientific.png",
		"/com/meraklisina/latexeditor/resources/icons/ic_letter.png",
		"/com/meraklisina/latexeditor/resources/icons/ic_table.png",
		"/com/meraklisina/latexeditor/resources/icons/ic_image.png"
    };
    
    String templateFiles[] = {
    	"src/com/meraklisina/latexeditor/resources/TemplateFiles/helloWorld.tex",
    	"src/com/meraklisina/latexeditor/resources/TemplateFiles/resume.tex",
    	"src/com/meraklisina/latexeditor/resources/TemplateFiles/scientific.tex",
    	"src/com/meraklisina/latexeditor/resources/TemplateFiles/letter.tex",
    	"src/com/meraklisina/latexeditor/resources/TemplateFiles/table.tex",
    	"src/com/meraklisina/latexeditor/resources/TemplateFiles/image.tex"
    };

	public TemplateForm(JDialog dialog, JTabbedPane editors)
    {
        this.topContainer = dialog;
        this.initComponents();
     }
	
	public void SetTextPane(JTextPane jtp)
	{
		this.textPane = jtp;
	}
	
	private void initComponents()
	{
		this.topContainer.setSize(new Dimension(500, 400));
		
		JTextPane explanationTextPane = new JTextPane();
		explanationTextPane.setEditable(false);
		
        JTextPane textPane = new JTextPane();
        textPane.setEditable(false);
        textPane.setBackground(new Color(255,255,255));
        textPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        
        StyledDocument doc = explanationTextPane.getStyledDocument();
        Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
        try {
        doc.insertString(doc.getLength(), "\nPlease select desired template below\n",  def);
        } catch (BadLocationException ble){
        	
        }
        
        doc = textPane.getStyledDocument();


        int i = 0;
                
        for(; i< templateNames.length; i++)
        {
        	buttons[i] = new JButton();
        	labels[i] = new JLabel(templateNames[i], new ImageIcon(getClass().getResource(templateImages[i]), "Templates"), SwingConstants.CENTER);
        	labels[i].setVerticalTextPosition(SwingConstants.BOTTOM);
        	labels[i].setHorizontalTextPosition(SwingConstants.CENTER);
        	buttons[i].add(labels[i]);
        	buttons[i].setActionCommand(Integer.toString(i));
        	buttons[i].addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonActionPerformed(evt);
                }
            });
        	
        	styles[i] = doc.addStyle("button" + i, def);
        	StyleConstants.setComponent(styles[i], buttons[i]);
        }
        
       try {
    	   doc.insertString(doc.getLength(), "\n",  def);
    	   doc.insertString(doc.getLength(), "  ",  def);
    	   for(i = 0; i < templateNames.length; i++)
    	   {
    		   if(i % 3 == 0 && i != 0)
    			   doc.insertString(doc.getLength(), "\n\n  ",  def);
    		   
    		   if(i % 3 != 0)
    			   doc.insertString(doc.getLength(), "\t",  def);
    		   
    		   doc.insertString(doc.getLength(), " ",  doc.getStyle("button" + i));
    	   }
    	   doc.insertString(doc.getLength(), "  ",  def);
    	   doc.insertString(doc.getLength(), "\n",  def);
        } catch (BadLocationException ble) {
        	ble.printStackTrace();
        }

        add(explanationTextPane);
        add(textPane);
    }
	
	private void buttonActionPerformed(java.awt.event.ActionEvent evt) 
	{
		int selectedTemplateNo = Integer.parseInt(evt.getActionCommand());
		String fileName = templateFiles[selectedTemplateNo];
		
		try{
			BufferedReader input =  new BufferedReader(new FileReader(new File(fileName)));
			
			String line;
	        while (( line = input.readLine()) != null)
			{
	        	textPane.getDocument().insertString(textPane.getDocument().getLength(), line + "\n", null);
			}
		}catch (Exception e){
		      System.err.println("Error: " + e.getMessage());
	    }
		
		topContainer.setVisible(false);
	}
}
